#include "loginwidget.h"
#include "ui_loginwidget.h"


#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QCryptographicHash>  // 添加加密头文件
#include <QByteArray>

#include "serialport.h"

#include "tcpclient.h"
#include "tcpserver.h"

#include "databasemanager.h"
#include <QTableView>

#include "udpserver.h"

loginWidget::loginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::loginWidget)
{
    ui->setupUi(this);
    TCpsever *ts = new TCpsever;
    TCPClientManager *tc = new TCPClientManager;
    udpserver *us = new udpserver;




    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    ui->pushButton_cast->setText("明文");



}

loginWidget::~loginWidget()
{
    delete ui;
}

// 密码MD5加密函数
QString loginWidget::md5Encrypt(const QString &password)
{
    QByteArray byteArray = password.toUtf8();
    QByteArray hash = QCryptographicHash::hash(byteArray, QCryptographicHash::Md5);
    return QString(hash.toHex()).toUpper();  // 转换为大写以匹配你的加密密码
}

void loginWidget::on_pushButton_cast_clicked()
{
    if(ui->lineEdit_password->echoMode() == QLineEdit::Password)
    {
        ui->pushButton_cast->setText("密文");
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->pushButton_cast->setText("明文");
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    }
}

void loginWidget::on_pushButton_login_clicked()
{
    QString username = ui->lineEdit_user->text();
    QString password = ui->lineEdit_password->text();

    // 对输入的密码进行MD5加密
    QString encryptedPassword = md5Encrypt(password);
    // 验证用户名和加密后的密码
//    if (login(username, encryptedPassword)){qDebug() << "登陆成功";}

//    if(username == "admin" && encryptedPassword =="21232F297A57A5A743894A0E4A801FC3")
    if (username == "admin" && password == "123456")
//    if(username == "admin" && password =="21232F297A57A5A743894A0E4A801FC3")
    {
        ui->label_tip->setText("登录成功！");
        emit loginSuccess();  // 发射登录成功信号
    }
    else
    {
        ui->label_tip->setText("登录失败，账号或密码错误！");
    }
}
